package com.hongawen.stream;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.hongawen.pojo.LimitRate;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2021年07月12日 20:03
 */
public class GroupStream {
    static List<LimitRate> list;

    static void prepareData() {
        list = new ArrayList<>();

        for (int i = 0; i < 20; i++) {
            LimitRate limitRate = new LimitRate();
            limitRate.setAllTime(RandomUtil.randomInt(1));
            limitRate.setMyIndex(1024 + i);
            limitRate.setTimeID(new Date());
            limitRate.setVoltageDevOvertime(0);
            limitRate.setUAberranceOverTime(1);
            list.add(limitRate);

            LimitRate limitRate1 = new LimitRate();
            limitRate1.setAllTime(RandomUtil.randomInt(1));
            limitRate1.setMyIndex(1024 + i);
            limitRate1.setTimeID(new Date());
            limitRate1.setVoltageDevOvertime(1);
            limitRate1.setUAberranceOverTime(1);
            limitRate1.setFlickerOverTime(1);
            list.add(limitRate1);
        }

    }

    public static void main(String[] args) {
        prepareData();
        Map<Integer, List<LimitRate>> collect = list.parallelStream().collect(Collectors.groupingBy(LimitRate::getMyIndex));

        Set<Map.Entry<Integer, List<LimitRate>>> entries = collect.entrySet();
        System.out.println("entry起始时间："+System.currentTimeMillis());
        List<Float> allData =  entries.stream().map(entry -> {
            float rate = 0.0f;
            List<LimitRate> limitRates = entry.getValue();
            if (CollectionUtil.isNotEmpty(limitRates)) {
                float count = (float) limitRates.stream().filter(limitRate -> limitRate.getFlickerOverTime() > 0 || limitRate.getFlicketAllTime() > 0).count();
                rate = count / limitRates.size();
            }
            return rate;
        }).collect(Collectors.toList());
        System.out.println("entry结束时间："+System.currentTimeMillis());

        //累加所有监测点的平均值
        Set<Integer> integers = collect.keySet();
        System.out.println("map起始时间："+System.currentTimeMillis());
        List<Float> allData2 = integers.stream().map(key -> {
            float rate = 0.0f;
            List<LimitRate> limitRates = collect.get(key);
            if (CollectionUtil.isNotEmpty(limitRates)) {
                float count = (float) limitRates.stream().filter(limitRate -> limitRate.getFlickerOverTime() > 0 || limitRate.getFlicketAllTime() > 0).count();
                rate = count / limitRates.size();
            }
            return rate;
        }).collect(Collectors.toList());
        System.out.println("map结束时间："+System.currentTimeMillis());
        double sum1 = allData.stream().mapToDouble(temp -> temp).sum();
        double sum2 = allData2.stream().mapToDouble(temp -> temp).sum();
        System.out.println(sum1 / collect.size());
        System.out.println(sum2 / collect.size());
    }


}
